package com.book.lizhi.read;

import java.util.Vector;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.TypedValue;
import android.view.View;
import android.widget.Scroller;

import com.book.lizhi.R;

public class ShowTextView extends View {

	private Paint mPaint;
	private Paint mBkPaint;
	private float mTextSize = 18;
	private float mLineSpac = 5;

	private Vector<String> mVector;
	private Scroller mScroller;

	private int mWidth;
	private int mHeight;
	private int mX;
	private int mY;

	// private Bitmap mBitmap;

	public ShowTextView(Context context) {
		super(context);
	}

	public ShowTextView(Context context, Paint paint, int color) {
		super(context);
		mPaint = paint;
		mTextSize = mPaint.getTextSize();

		mBkPaint = new Paint();
		mBkPaint.setColor(color);

		// targetSdkVersion > 10 无效。需要设置 setLayerType(View.LAYER_TYPE_SOFTWARE,null);
		mBkPaint.setShadowLayer(30, 5, 5, Color.DKGRAY);

		mScroller = new Scroller(context);

		mWidth = getResources().getDisplayMetrics().widthPixels;
		mHeight = getResources().getDisplayMetrics().heightPixels;

		mX = getResources().getDimensionPixelSize(R.dimen._20);
		mY = getResources().getDimensionPixelSize(R.dimen._40);

		// mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.library_backgroud);
	}

	public void setBackgroud(int color) {
		mBkPaint.setColor(color);
		invalidate();
	}

	public Vector<String> getText() {
		return mVector;
	}

	public void setText(Vector<String> strings) {
		mVector = strings;
		invalidate();
	}

	public void setBackgroud(Bitmap bitmap) {

	}

	public void setTextSize(float size) {
		Context context = getContext();
		Resources resources;
		if (context == null) {
			resources = Resources.getSystem();
		} else {
			resources = context.getResources();
		}

		mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, size, resources.getDisplayMetrics());
		mPaint.setTextSize(mTextSize);
		requestLayout();
		invalidate();
	}

	public int getLineHeight() {
		return (int) (mTextSize + mLineSpac);
	}

	public void nextScroll(int x, int y) {
		mScroller.startScroll(x, 0, mWidth - x, 0, 300);
		postInvalidate();
	}

	public void preScroll(int x, int y) {
		// 滚动从控件左侧开始
		mScroller.startScroll(mWidth - x, 0, x - mWidth, 0, 300);
		postInvalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {

		canvas.drawRect(0, 0, mWidth, mHeight, mBkPaint);

		int y = mY;
		if (mVector != null && mVector.size() != 0) {
			for (String string : mVector) {
				canvas.drawText(string, mX, y, mPaint);
				y += (mTextSize + mLineSpac);
			}
		}
	}

	public Paint getPaint() {
		return mPaint;
	}

	public void resetPosition() {
		mScroller.startScroll(0, 0, 0, 0);
		postInvalidate();
	}

	@Override
	public void computeScroll() {
		if (mScroller.computeScrollOffset()) {
			scrollTo(mScroller.getCurrX(), 0);
			postInvalidate();
		}

	}

	public void setPaint(Paint paint) {
		mPaint = paint;
		invalidate();
	}
}
